﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB.Driver;
using System.Collections;

namespace IFTRUE.EIMS.BLL
{
    /// <summary>
    /// 作用：日志管理类
    /// 作者：iftrue
    /// 日期：2013-08-19
    /// </summary>
   public class Log
    {
      private MongoCollection collection;
      public Log()
      {
          collection = MongoDbFactory.MongoDatabase.GetCollection("Log");
      }
       /// <summary>
       /// 添加日志
       /// </summary>
       /// <param name="logModel">日志实体</param>
       /// <returns></returns>
       public bool Add(Model.Log logModel)
       {
           var result = collection.Insert<Model.Log>(logModel);
           return result.Ok;
       }

       /// <summary>
       /// 删除日志
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
       public bool Delete(string id)
       {
           var result = collection.Remove(MongoDB.Driver.Builders.Query.EQ("_id", new MongoDB.Bson.ObjectId(id)));
           return result.Ok;
       }

       /// <summary>
       /// 获取日志列表
       /// </summary>
       /// <returns></returns>
       public MongoDB.Driver.MongoCursor<Model.Log> GetList()
       {
           var result = collection.FindAllAs<Model.Log>();
           return result;
       }

       /// <summary>
       /// 获取分页数据
       /// </summary>
       /// <param name="currIndex">当前页索引</param>
       /// <param name="pageSize">页行数</param>
       /// <param name="current">查询条件</param>
       /// <returns></returns>
       public string GetDataByPage(int currIndex, int pageSize, Hashtable ht)
       {
           MongoDB.Driver.IMongoQuery []querys = null;
           if (ht != null && ht.Count != 0)
           {
               querys = new MongoDB.Driver.QueryDocument[ht.Count];
               int i = 0;
              foreach(DictionaryEntry de in ht)
              {
                   if (de.Key.ToString() == "Type")
                   {
                       querys[i] = MongoDB.Driver.Builders.Query.EQ("Type", Convert.ToInt32(de.Value.ToString()));
                   }
                   else if (de.Key.ToString() == "CreateDate")
                   {
                       querys[i] = MongoDB.Driver.Builders.Query.GTE("CreateDate", Convert.ToDateTime(de.Value.ToString()));
                   }
                   else if (de.Key.ToString() == "CreateDateEnd")
                   {
                       querys[i] = MongoDB.Driver.Builders.Query.LTE("CreateDate",Convert.ToDateTime( de.Value.ToString()));
                   }
                   i++;
               }
           }
           long recordCount = 0;
           var result = new Common().GetMongoCursorByPage<Model.Log>(collection, (querys==null)?null:MongoDB.Driver.Builders.Query.And(querys), MongoDB.Driver.Builders.SortBy.Descending("CreateDate"), currIndex, pageSize,ref recordCount);
           StringBuilder s = new StringBuilder();
           if (result!=null)
           {
               foreach(var v in result)
               {
                   string strLogType = "其它日志";
                   switch (v.Type)
                   { 
                       case 1:
                           strLogType = "系统日志";break;
                       case 2: strLogType = "操作日志"; break;
                       case 3: strLogType = "错误日志"; break;
                       case 4: strLogType = "异常日志"; break;
                   }
                   s.Append(string.Format("<tr target='pid' rel='" + v.Id + "'><td>{0}</td><td>{1}</td><td>{2}</td><td title='{3}'>{4}</td><td>{5}</td><td>{6}</td></tr>",
                       "<input name='ids' value='" + v.Id + "' type='checkbox'>", strLogType, v.Title, v.Content, v.Content, v.IP, v.CreateDate.ToString("yyyy-MM-dd HH:mm:ss")));
               }
           }
           return recordCount.ToString() + "|" + s.ToString();
       }
    }
}
